package com.fh.controller.cg.product;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.Product.Product;
import com.fh.service.cg.baseinfo.BaseGroupService;
import com.fh.service.cg.product.ProductService;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.Jurisdiction;
import com.fh.util.PageData;
@Controller
@RequestMapping(value="/product")
public class ProductController extends BaseController{

	String menuUrl = "product/list.do"; //菜单地址(权限用)
	
	@Resource(name="productService")
	private ProductService productService;
	
	@Resource(name="baseGroupService")
	private BaseGroupService baseGroupService;
	
	/**
	 * 新增
	 */
	@RequestMapping(value="/save")
	public ModelAndView save() throws Exception{
		
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;}
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("PRODUCTID", this.get32UUID());	//主键
//		pd.put("CREATETIME", Tools.date2Str(new Date())); //创建时间
		productService.save(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out){
	
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;}
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			productService.delete(pd);
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
	}
	/**
	 * 修改
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit() throws Exception{
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;}
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		productService.edit(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return null;}
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				productService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	/*
	 * 列表页 加载	
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page) throws Exception
	{
		//1创建modeanyview
		ModelAndView mv = this.getModelAndView();
		//2创建PageData
		PageData  pd = new PageData();

		//3设置pageData所要传的值
		pd=this.getPageData();
		String PRODUCTNAME = pd.getString("PRODUCTNAME");
		//4.判断传值是否为空
		if(null!=PRODUCTNAME && !"".equals(PRODUCTNAME))
		{
			pd.put("PRODUCTNAME", PRODUCTNAME.trim());
		}

		//5.把PD的值传给Page去做分页
		page.setPd(pd);

		//6.根据去数据库查询数据
		List<PageData> varList = productService.list(page);	//列出Textmsg列表
		//封装返回参数
		mv.setViewName("cgsystem/product/productmsg_list");//设置返回哪个jsp

		mv.addObject("varList", varList);//返回数据库查询的数据


		mv.addObject("pd", pd);//返回PD参数

		mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限

		return mv;
	}

	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		
 		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
		    List<PageData> listAll = baseGroupService.listAll(pd);	//查询所有部门
			mv.setViewName("cgsystem/product/productmsg_edit");
			mv.addObject("msg", "save");
			mv.addObject("item", listAll);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = productService.findById(pd);	//根据ID读取
			List<PageData> listAll = baseGroupService.listAll(pd);	//查询所有部门
			
			mv.setViewName("cgsystem/product/productmsg_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
			mv.addObject("item", listAll);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
